import Pyro.core
import sys
import uuid

class RemoteObjectFactory(Pyro.core.ObjBase):
    def __init__(self):
        Pyro.core.ObjBase.__init__(self)

    def create(self, d, moduleDotClassName, ObjName):
        moduleName, className = moduleDotClassName.split('.')
        __import__(moduleName)
        module = sys.modules[moduleName]
        
        pyroObj = getattr(module, className)()
        pyroObjName = ObjName + '|' + str(uuid.uuid1())
        d.connect(pyroObj, pyroObjName)
        
        return pyroObj.getProxy()
        
    def createMany(self, d, nrObjects, moduleDotClassName, ObjName, attrProxy = 'false'):
        moduleName, className = moduleDotClassName.split('.')
        __import__(moduleName)
        module = sys.modules[moduleName]
            
        result = []
        for i in range(nrObjects):
            pyroObj = getattr(module, className)()
            pyroObjName = ObjName + '|' + str(uuid.uuid1())
            d.connect(pyroObj, pyroObjName)
            if attrProxy:
                result.append(pyroObj.getAttrProxy())
            else:
                result.append(pyroObj.getProxy())
        return result